# -*- coding: utf-8 -*-
# @File     : handle_excel.py
# @Project  : letCodeCase
# @Author   : 熊剑文
# @Time     : 2024/9/5 16:30:29
# @Copyright: 江西博微新技术有限公司
import json

from configs.env import Env

pass

"""
模块描述：
    
"""
from utils.handle_path import LetCodePath
import pandas
from typing import List

def get_excel_case_data(excel_filename,
                        rows=None,
                        columns: List=None,
                        sheetname='',):
    df = pandas.read_excel(LetCodePath.testDatas_path / excel_filename,
                           sheet_name=sheetname)
    if rows:
        return df[df.用例编号.isin(rows)][columns].values
    else: return df.values

import pandas as pd
import json

class Excel_Obj:
    def __init__(self, file, sheet):
        self.file = file
        self.sheet = sheet
        self.df = pd.read_excel(self.file, sheet_name=sheet)

    def filter_excel_data(self, case_name, run_case=None):
        """
        根据Env中的优先级设置，以及run_case的范围来筛选测试用例数据
        :param case_name: 用例的名称(前缀)，如：Login
        :param run_case: 执行用例的编号，可以是多种格式，如all，001-003，004
        :return: [(),()]
        """
        if run_case is None:
            run_case = ['all']

        # 存放结果，格式[(),()]
        res_list = []

        # 筛选出执行run_case的用例
        run_case_data = []  # 根据run_case，得到用例编号的列表(包含用例名称+编号)
        if 'all' in run_case:
            run_case_data = self.df.iloc[:, 0].tolist()
        else:
            for one in run_case:
                if '-' in one:
                    start, end = one.split('-')
                    run_case_data.extend([case_name + f'{num:0>3}' for num in range(int(start), int(end)+1)])
                else:
                    run_case_data.append(case_name + f'{one:0>3}')

        # 筛选出符合优先级的用例
        for index, row in self.df.iterrows():
            one = row[0]
            if case_name in one and one in run_case_data:
                severity_cell = row[2]  # 优先级在第2列
                col_indexs = [0, 2, 3, 4]  # 需要的列索引
                col_datas = []
                for severity in Env.TESTCASE_SEVERITY:
                    if severity_cell == severity:
                        for num in col_indexs:
                            tmp = row[num]
                            temp = json.loads(tmp) if '{' in tmp else tmp
                            col_datas.append(temp)
                        res_list.append(tuple(col_datas))
                        break  # 找到匹配的优先级后跳出循环

        return res_list

# 使用示例
# excel_obj = Excel_Obj('path_to_your_excel_file.xlsx', 'Sheet1')
# result = excel_obj.filter_excel_data('Login', ['001-003', '005'])



if __name__ == '__main__':
    print(get_excel_case_data('letCodeData.xlsx',
                              rows=['输入模块'],
                              columns=['优先级','用例标题','输入内容','定位方式','定位表达式','预期结果'],
                              sheetname='Input模块'))